package com.virgil.core.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;

import com.virgil.core.utils.JwtUtil;

public class BaseController {

	@Resource
	HttpServletRequest request;

	/**
	 * 获取请求头的token
	 *
	 * @return token
	 */
	protected String getToken() {
		String token = request.getHeader("token");
		return StringUtils.hasText(token) ? token : "";
	}

	/**
	 * 获取用户id
	 *
	 * @return userId
	 */
	protected Long getUserId() {
		String token = request.getHeader("token");
		Long userId = JwtUtil.validateToken(token);
		if (userId == null) {
			return 0L;
		}
		return userId;
	}
}
